﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LineStickerDwonload.Linehelper
{
    internal class LineTool
    {  /// <summary>
       /// 封装文件下载信息
       /// </summary>
        public struct DownloadInfo
        {
            private string fileLink;
            private string fileName;

            public string FileLink { get => fileLink; set => fileLink = value; }
            public string FileName { get => fileName; set => fileName = value; }
        }

        /// <summary>
        /// 获取图片ID
        /// </summary>
        /// <param name="ImgLink"></param>
        /// <returns></returns>
        public static string GetImgId(string ImgLink)
        {
            string ImgID = TextHelper.ReplaceTextEnhance(ImgLink, @"https://[\w\W].*?/sticker/", "");
            ///
            ImgID = TextHelper.ReplaceTextEnhance(ImgID, @"/[\w\W].*?/[\w\W].*?.(png|jpg)", "");
            return ImgID;
        }

        /// <summary>
        /// 获取文件夹路径
        /// </summary>
        /// <param name="Shtml"></param>
        /// <returns></returns>
        public static string GetFloderPath(string Shtml)
        {
            string FloderPath = $"{StaticValue.TempPath}{TextHelper.ReplaceBadChar(new Transcoding().HtmlDecodeToUtf8(HtmlHelper.GetFirstText(Shtml, "//title")))}\\";
            FloderHelper.FloderExits(FloderPath, true);
            return FloderPath;
        }

        /// <summary>
        /// 分析图片结果
        /// </summary>
        /// <param name="result"></param>
        public static List<LineTool.DownloadInfo> AnalysisImg(string result)
        {
            List<LineTool.DownloadInfo> ImgList = new List<LineTool.DownloadInfo>();
            foreach (String line in HtmlHelper.GetAttributesVlaueList(result, "data-preview", "//li[@class='mdCMN09Li FnStickerPreviewItem']"))
            {
                String ImgLink = "";
                string ImgID = "";
                string GifLink = "";
                string SoundLink = "";
                try
                {
                    LineStickerStaticvalue.Root root =
                        JsonConvert.DeserializeObject<LineStickerStaticvalue.Root>(Transcoding.HtmlDecode(line));
                    ImgID = root.id;
                    ImgLink = root.staticUrl;
                    GifLink = root.animationUrl;
                    SoundLink = root.soundUrl;
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                }

                if (ImgLink != "")
                    ImgList.Add(new DownloadInfo { FileLink = ImgLink, FileName = ImgID + ".png" });
                if (SoundLink != "")
                    ImgList.Add(new DownloadInfo { FileLink = SoundLink, FileName = ImgID + ".m4a" });
                if (GifLink != "")
                    ImgList.Add(new DownloadInfo { FileLink = GifLink, FileName = ImgID + ".apng" });
            }
            return ImgList;
        }
    }
}